using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
using System.Xml;

using IGlobalist.Chat.Engine;
using IGlobalist.Cms.Chat;

namespace IGlobalist.Cms.Common
{
    public class RoomConfig
    {
        private static string roomConfigFilePath = "Room.config";
        private static XmlDocument xDoc;

        static RoomConfig()
        {
             roomConfigFilePath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, roomConfigFilePath);
             
        }

        public static int Count
        {
            get
            {
                LoadXML();
                string xpath = "//room";
                XmlNodeList xNodes = xDoc.DocumentElement.SelectNodes(xpath);
                return xNodes.Count;
            }
        }

        private static User GetUserAdmin(string username, string password, string roomID)
        {
            User user = new User(ChatBasePage.GetUserID(username, roomID), username, IGlobalist.Chat.Engine.Common.UserType.Admin);
            user.Password = password;
            return user;
        }

        
        public static Room GetChatRoom(string roomID)
        {
            try
            {
                Room chatRoom = new Room(roomID);
                LoadXML();               
                string xpath = String.Format("//room[@id='{0}']", roomID);
                XmlNode xNode = xDoc.DocumentElement.SelectSingleNode(xpath);
                chatRoom.Name = xNode.Attributes["name"].Value;
                
                string username=xNode.Attributes["admin"].Value;
                string password=xNode.Attributes["password"].Value;
                chatRoom.Administrator = GetUserAdmin(username, password, roomID);

                return chatRoom;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        
        public static void Save()
        {
            lock (xDoc)
            {
                xDoc.Save(roomConfigFilePath);                
            }
        }

         #region Load Config
         private static void LoadXML()
         {
             if (xDoc != null) return;
             xDoc = new XmlDocument();
             lock (xDoc)
             {
                 xDoc.Load(roomConfigFilePath);
                 try
                 {

                     HttpContext.Current.Cache.Add(roomConfigFilePath, xDoc, new System.Web.Caching.CacheDependency(roomConfigFilePath), DateTime.Now.AddYears(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, new System.Web.Caching.CacheItemRemovedCallback(CacheRemoveCallback));
                 }
                 catch (Exception e)
                 {
                     throw e;
                 }
             }
         }

         private static void CacheRemoveCallback(string key, object obj, System.Web.Caching.CacheItemRemovedReason reason)
         {
             xDoc = null;
             LoadXML();
         }
        #endregion
    }
}
